package jsi.ws;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Dict {

	Log log = LogFactory.getLog(this.getClass());
	
	private static Dict dict;
	Map<String, String> mappings;
	
	public Dict() {
		try {
			init();
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
	
	protected void init() throws IOException{
		Date startd = new Date();
		mappings = new HashMap<String, String>();
		InputStream is = new FileInputStream(new File("/home/jsitek/soft/sjp/odm.txt"));
		InputStreamReader r = new InputStreamReader(is, "iso-8859-2");
		
		BufferedReader in = new BufferedReader(r);
		String line = null;
		int i = 0;
		while ((line = in.readLine()) != null){
			String [] words = line.split(",");
			String base = new String(words[0]).trim();
			for (int j = 1; j < words.length; j++){
				mappings.put(words[j].trim(), base);
			}
			i++;
			if (i%30000 == 0) {
				log.debug(i);;
			}
		}
	}

	public String basic(String derivate){
		String basic = mappings.get(derivate);
		log.debug(derivate + "  ->\t" + basic);
		return basic;
	}
	
	public static Dict getDict(){
		try {
			if (dict==null){
				dict = new Dict();
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		return dict;
	}
}
